package org.popupcommunication.controller;

import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.popupcommunication.utils.BrowserFiles;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("FileManager")
public class FileManagerController {
	
	   @RequestMapping(value = "/show", method = RequestMethod.GET)
	    public String displayForm(HttpServletRequest request,Model model) throws URISyntaxException {
		   Path rep = null;
		   List<String> lst = null;

		   String repertoire = request.getSession().getServletContext().getRealPath("/resources/upload/");
		   rep = Paths.get(repertoire);
		 	String rac = request.getSession().getServletContext().getRealPath("/");
			Path racine = Paths.get(rac);		   
		   try {
			 lst = BrowserFiles.getDirectoryFiles(rep,racine);
			 System.out.println("lst "+lst);

		} catch (IOException e) {
			e.printStackTrace();
		}
		   model.addAttribute("fichiers", lst);
	        return "filemanager/upload_form";
	    }
	
}
